package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mmall.common.ResponseInfo;
import com.mmall.common.ServerResponse;
import com.mmall.dao.ShippingMapper;
import com.mmall.pojo.Shipping;
import com.mmall.service.IShippingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 收货地址模块Service层实现
 * Created by qezhhnjy on 2017/10/3-11:44.
 */
@Service("iShippingService")
public class ShippingServiceImpl implements IShippingService {

    @Resource
    private ShippingMapper shippingMapper;

    //新增收货地址
    @Override
    public ServerResponse add(Integer userId, Shipping shipping) {
        if (userId == null || shipping == null)
            return ServerResponse.createByErrorCode(ResponseInfo.ILLEGAL_ARGUMENT.getCode(), ResponseInfo.ILLEGAL_ARGUMENT.getDescription());
        //这里提供setUserId方法一方面为了指定用户id.一方面为了防止前端传递userId为其他用户的shipping对象到sql语句中越权.
        shipping.setUserId(userId);
        int insertCount = shippingMapper.insert(shipping);
        if (insertCount > 0) {
            Map<String, Integer> idInfo = Maps.newHashMap();
            idInfo.put("shippingId", shipping.getId());
            return ServerResponse.createBySuccess("新建地址成功~!", idInfo);
        }
        return ServerResponse.createByErrorMessage("新建地址失败~!");
    }

    //删除收货地址
    @Override
    public ServerResponse delete(Integer userId, Integer shippingId) {
        //检测参数是否为空
        if (userId == null || shippingId == null)
            return ServerResponse.createByErrorCode(ResponseInfo.ILLEGAL_ARGUMENT.getCode(), ResponseInfo.ILLEGAL_ARGUMENT.getDescription());
        //这里不能直接执行删除语句,这样会出现横向越权的安全漏洞.需要先判定shipping的用户id是否与传递过来的userId对应.所以不能直接使用deleteByPrimaryKey方法.
        int deleteCount = shippingMapper.deleteByIdUserId(userId, shippingId);
        if (deleteCount > 0)
            return ServerResponse.createBySuccessMessage("删除地址成功");
        return ServerResponse.createByErrorMessage("删除地址失败~!");
    }

    //更新指定用户的指定收货地址.需要指定sql中指定用户ID.
    @Override
    public ServerResponse update(Integer userId, Shipping shipping) {
        if (userId == null || shipping == null)
            return ServerResponse.createByErrorCode(ResponseInfo.ILLEGAL_ARGUMENT.getCode(), ResponseInfo.ILLEGAL_ARGUMENT.getDescription());
        //这里依然不能直接使用mybatis自动生成的updateByPrimaryKey()方法.需要在sql语句中添加userId的判定,并删除userId的赋值项.因为更新并不更新userId.
        shipping.setUserId(userId);
        int updateCount = shippingMapper.updateByShipping(shipping);
        if (updateCount > 0)
            return ServerResponse.createBySuccessMessage("更新地址成功~!");
        return ServerResponse.createByErrorMessage("更新地址失败~!");
    }

    //查询当前用户的单个收货地址信息
    @Override
    public ServerResponse<Shipping> select(Integer userId, Integer shippingId) {
        if (userId == null || shippingId == null)
            return ServerResponse.createByErrorCode(ResponseInfo.ILLEGAL_ARGUMENT.getCode(), ResponseInfo.ILLEGAL_ARGUMENT.getDescription());
        Shipping shipping = shippingMapper.selectByIdUserId(userId, shippingId);
        if (shipping == null)
            return ServerResponse.createByErrorMessage("查询失败,请重新输入参数~!");
        return ServerResponse.createBySuccess("查询成功", shipping);
    }

    //查询当前用户的全部收货地址信息
    @Override
    public ServerResponse<PageInfo<Shipping>> list(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Shipping> shippings = shippingMapper.selectByUserId(userId);
        PageInfo<Shipping> pageInfo = new PageInfo<>(shippings);
        return ServerResponse.createBySuccess(pageInfo);
    }
}
